package com.example.foods.controller;

import com.example.foods.domain.AjaxResult;
import com.example.foods.domain.FoodsInfo;
import com.example.foods.service.OrderItemService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/orderItems")
public class OrderItemController {

    @Resource
    private OrderItemService orderItemService;

    //查询所有购物项
    @GetMapping("/list")
    public AjaxResult list() {
        return AjaxResult.ok(this.orderItemService.list());
    }

    //删除
    @GetMapping("/delete/{id}")
    public void delete(@PathVariable int id, HttpServletResponse response)
            throws IOException {
        this.orderItemService.delete(id);
        //重定向
        String redirectUrl = "http://localhost:8080/orders/order_item_list.html";
        response.sendRedirect(redirectUrl);
    }

    //加入订单项（购物车）
    @PostMapping("/addCart")
    public AjaxResult addCart(@RequestBody FoodsInfo foodsInfo){
        orderItemService.addCart(foodsInfo);
        return AjaxResult.ok(true);
    }

}
